// 历史更新内容的仓库
import delay from "@/utils/delay";
export default {
  namespaced: true, // 开启命名空间
  state: {
    // 是否已经登录 <false>
    isLogin: false,
    // 从cos中获取的用户信息
    user: {
      userName: "",
      cards: "",
    },
    // 每关的排行
    ranking: {
      1: [
        // ["零泠丶", 23], // 测试用数据
        // ["羊了个1", 25],
        // ["羊了个2", 26],
        // ["羊了个3", 28],
        // ["羊了个4", 30],
        // ["羊了sdfdsdfsfsf个", 33],
        // ["羊了个6", 34],
        // ["羊了个7", 36],
        // ["羊了个8", 39],
        // ["羊了个9", 41],
        // ["羊了个10", 42],
        // ["羊了个11", 43],
        // ["羊了个12", 48],
        // ["羊了个13", 53],
        // ["羊了个14", 56],
        // ["羊了个15", 59],
        // ["羊了个16", 61],
        // ["羊了个17", 69],
        // ["羊了个18", 79],
        // ["羊了个20", 80],
        // ["羊了个110", 109],
        // ["羊了个111", 123],
        // ["羊了个112", 125],
        // ["羊了个113", 128],
        // ["羊了个114", 135],
        // ["羊了个115", 145],
        // ["羊了个116", 155],
        // ["羊了个117", 155],
        // ["羊了个118", 157],
        // ["羊了个120", 190],
      ],
      2: [],
      3: [],
      4: [],
      5: [],
      6: [],
      7: [],
      8: [],
      9: [],
      10: [],
    },
    // 每关的最大上榜秒数 [小于则可上榜]
    rankingMax: {
      // 计算方式: levelNum * 1.3 * 60 - 30
      1: 60,
      2: 120,
      3: 180,
      4: 240,
      5: 300,
      6: 360,
      7: 420,
      8: 480,
      9: 540,
      10: 600,
    },
  },
  mutations: {
    // 设置是否已经登录
    setIsLogin(state, payload) {
      state.isLogin = payload;
    },
    // 设置登录者的用户信息
    setUser(state, payload) {
      // {userName: "xxx", cards: "xxx"}
      state.user = payload;
    },
    // 设置每关的排行
    setRanking(state, payload) {
      state.ranking = payload;
    },
    // 设置每关的最低上榜秒数
    setRankingMax(state, payload) {
      state.rankingMax = payload;
    },
    // 在排行榜的当前关卡中插入一个数据
    addRanking(state, payload) {
      // {levelNum:xxx,userName:xxx,time:xxx} [当前关卡,用户名,用时(秒)]
      const curRanking = state.ranking[payload.levelNum]; // 当前关卡的ranking
      console.log(state.ranking);
      if (curRanking.length === 0) {
        curRanking[0] = [payload.userName, payload.time];
        return;
      }
      for (let i = curRanking.length - 1; i >= 0; i--) {
        const item = curRanking[i];
        if (i + 1 < 30) {
          if (payload.time < item[1]) {
            curRanking[i + 1] = curRanking[i]; // 后移一位
          } else {
            // 插入到合适的位置
            // curRanking[i + 1] = curRanking[i]; // 后移一位
            curRanking[i + 1] = [payload.userName, payload.time];
            break;
          }
        }
        if (i === 0) {
          curRanking[0] = [payload.userName, payload.time];
          break;
        }
      }
    },
    // 在排行榜的当前关卡中更新自己的数据
    updateMyRanking(state, payload) {
      // {levelNum:xxx,i:xxx,time:xxx} [关卡数,自己的索引,用时(秒)]
      state.ranking[payload.levelNum].splice(payload.i, 1); // 删自己
      this.commit("requestData/addRanking", {
        levelNum: payload.levelNum,
        userName: state.user.userName,
        time: payload.time
      });
    },
  },
  actions: {},
};
